from flask import Blueprint, request, jsonify, json
from flask_restful import Resource, Api, reqparse, marshal, marshal_with
from common.utils.myredis import myredis
from second_flask.methods import db1
from common.utils.get_data import get_data, _get_data

goods_info_bp = Blueprint('goods_info_bp', __name__)

api = Api(goods_info_bp)


class GetGoodsInfo(Resource):
    def get(self):
        sql = "select * from goods"
        goods_list = db1.find_all(sql)
        return jsonify({'code': 200, 'goods_list': goods_list})


class AddGoods(Resource):
    def post(self):
        goods_name = get_data()['goods_name']
        price = get_data()['price']
        url = get_data()['url']
        print('url>>>>>', url)
        content = get_data()['content']
        stock = get_data()['stock']
        lock_stock = get_data()['lock_stock']
        sale_num = get_data()['sale_num']
        view_num = get_data()['view_num']
        appraise_num = get_data()['appraise_num']
        sql = "insert into goods(goods_name,price,img,content,stock,lock_stock,sale_num,view_num,appraise_num) values('%s','%s','%s','%s',%d,%d,%d,%d,%d)" % (
            goods_name, price, url, content, int(stock), int(lock_stock), int(sale_num), int(view_num),
            int(appraise_num))
        db1.update(sql)
        return jsonify({'code': 200})


class DelGoods(Resource):
    def put(self):
        id = get_data()['id']
        sql = "delete from goods where id=%d" % (int(id))
        db1.update(sql)
        return jsonify({'code': 200})


class GetOneGoods(Resource):
    def get(self):
        id = _get_data()['id']
        sql = "select * from goods where id=%d" % (int(id))
        goods = db1.find_all(sql)
        return jsonify({'code': 200, 'goods': goods})


class UpdateGoods(Resource):
    def put(self):
        id = get_data()['id']
        goods_name = get_data()['goods_name']
        price = get_data()['price']
        url = get_data()['url']
        print('url>>>>>', url)
        content = get_data()['content']
        stock = get_data()['stock']
        lock_stock = get_data()['lock_stock']
        sale_num = get_data()['sale_num']
        view_num = get_data()['view_num']
        appraise_num = get_data()['appraise_num']
        sql = "update goods set goods_name='%s',price='%s',img='%s',content='%s',stock=%d,lock_stock=%d,sale_num=%d,view_num=%d,appraise_num=%d where id=%d" % (
            goods_name, price, url, content, int(stock), int(lock_stock), int(sale_num), int(view_num),
            int(appraise_num),
            int(id))
        db1.update(sql)
        return jsonify({'code': 200})


class GetInfos(Resource):
    def get(self):
        sql = "select * from info"
        info_list = db1.find_all(sql)
        return jsonify({'code': 200, 'info_list': info_list})


class AddGoodsInfo(Resource):
    def post(self):
        id = get_data()['id']
        info_list = get_data()['info_list']
        sql = "select info_id from goods_info where goods_id=%d" %(int(id))
        res = db1.find_all(sql)
        lis = []
        for k in res:
            lis.append(k['info_id'])
        set1 = set(info_list) - set(lis)
        if len(set1) == 0:
            return jsonify({'code': 403, 'message': '已存在'})
        else:
            lis = list(set1)
            print(lis)
            for i in lis:
                sql = "insert into goods_info values(%d,%d)" % (int(i), int(id))
                db1.update(sql)
            return jsonify({'code': 200})



api.add_resource(GetGoodsInfo, '/get_goods_info', endpoint='get_goods_info')
api.add_resource(AddGoods, '/add_goods', endpoint='add_goods')
api.add_resource(DelGoods, '/del_goods', endpoint='del_goods')
api.add_resource(GetOneGoods, '/get_one_goods', endpoint='get_one_goods')
api.add_resource(UpdateGoods, '/update_goods', endpoint='update_goods')
api.add_resource(GetInfos, '/get_infos', endpoint='get_infos')
api.add_resource(AddGoodsInfo, '/add_goods_info', endpoint='add_goods_info')
